/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
    SPDX-FileCopyrightText: 2021 Marco Martin <mart@kde.org>
    SPDX-FileCopyrightText: 2018 Furkan Tokac <furkantokac34@gmail.com>
    SPDX-FileCopyrightText: 2019-2022 Nate Graham <nate@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
import QtQuick 2.7
import QtQuick.Controls 2.5 as QQC2
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
import org.kde.kirigami 2.13 as Kirigami
// import org.kde.kirigami 2.13 as Kirigami2
import org.kde.kcm 1.4 as KCM
import org.kde.plasma.core 2.0 as PlasmaCore
import QtGraphicalEffects 1.15

Kirigami.Page {
    id: root
    title: i18n("Quick Settings")
    implicitWidth: Kirigami.Units.gridUnit * 44
    implicitHeight: Kirigami.Units.gridUnit * 45

    property bool lighttheme:PlasmaCore.Theme.viewBackgroundColor=="#ffffff"
    property var backgroundcolor: PlasmaCore.Theme.viewBackgroundColor
    property var textcolor:PlasmaCore.Theme.textColor
    property var bordercolor:lighttheme?"#CCCCCC":"#2D2D2D"
    property var spaceline:lighttheme?"#F0F0F0":"#2D2D2D"

    Item {
        id: content
        anchors.fill: parent

        // theme
        Rectangle {
            id:theme
            anchors.horizontalCenter:parent.horizontalCenter
            color: "transparent"
            height:20
            width:558
            Text {
                anchors.left: parent.left
                text: i18nc("landingpages theme", "Theme")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
        }
        Rectangle {
            id:themeIcon
            anchors.top: theme.bottom
            anchors.topMargin: 4
            anchors.horizontalCenter:parent.horizontalCenter
            color: backgroundcolor
            height:101
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10

            QQC2.ButtonGroup { id: themeGroup }

            Thumbnail {
                id: thumbnail1
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 23

                imageSource: kcm.defaultLightLookAndFeel.thumbnail
                text: kcm.defaultLightLookAndFeel.name
                checked: kcm.globalsSettings.lookAndFeelPackage === kcm.defaultLightLookAndFeel.id
                QQC2.ButtonGroup.group: themeGroup

                onToggled: kcm.globalsSettings.lookAndFeelPackage = kcm.defaultLightLookAndFeel.id

                KCM.SettingStateBinding {
                    configObject: kcm.globalsSettings
                    settingName: "lookAndFeelPackage"
                }
            }
            Thumbnail {
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: thumbnail1.right
                anchors.leftMargin: 23

                imageSource: kcm.defaultDarkLookAndFeel.thumbnail
                text: kcm.defaultDarkLookAndFeel.name
                checked: kcm.globalsSettings.lookAndFeelPackage === kcm.defaultDarkLookAndFeel.id
                QQC2.ButtonGroup.group: themeGroup

                onToggled: kcm.globalsSettings.lookAndFeelPackage = kcm.defaultDarkLookAndFeel.id

                KCM.SettingStateBinding {
                    configObject: kcm.globalsSettings
                    settingName: "lookAndFeelPackage"
                }
            }
        }

        //Animation speed
        Rectangle {
            id: animationSpeed
            anchors.top: themeIcon.bottom
            anchors.topMargin: 6
            anchors.horizontalCenter:parent.horizontalCenter
            color: backgroundcolor
            height:48
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10

            Text {
                anchors.top: parent.top
                anchors.topMargin: 16
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18n("Animation speed")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }

            KcmSlider {
                id: slider
                anchors.top: parent.top
                anchors.topMargin: 25
                anchors.left: parent.left
                anchors.leftMargin:342
                from: -4
                to: 4

                stepSize: 0.5
                snapMode: QQC2.Slider.SnapAlways
                value: (kcm.globalsSettings.animationDurationFactor === 0)
                       ? slider.to
                       : -(Math.log(kcm.globalsSettings.animationDurationFactor) / Math.log(2))

                KCM.SettingStateBinding {
                    configObject: kcm.globalsSettings
                    settingName: "animationDurationFactor"
                }
                onValueChanged: {
                    kcm.globalsSettings.animationDurationFactor =
                            (value === to) ? 0 : (1.0 / Math.pow(2, value))
                }
            }
        }

        //appearance and more settings
        Rectangle {
            id: appearance
            anchors.top: animationSpeed.bottom
            anchors.topMargin: 6
            anchors.horizontalCenter:parent.horizontalCenter
            color: backgroundcolor
            height:95
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10

            clip: true
            layer.enabled: true
            layer.effect: OpacityMask {
                maskSource: Rectangle {
                    width: appearance.width
                    height: appearance.height
                    radius: appearance.radius
                }
            }

            // appearance
            Rectangle {
                id: appearanceRect
                color: "transparent"
                anchors.top: parent.top
                anchors.left: parent.left
                width: parent.width
                height: 47

                Kirigami.Icon {
                    id: appearanceIcon
                    anchors.verticalCenter: appearanceRect.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 23

                    height: 16
                    width: 16
                    source: "preferences-desktop-wallpaper"
                }

                Text {
                    anchors.verticalCenter: appearanceRect.verticalCenter
                    anchors.left: appearanceIcon.right
                    anchors.leftMargin: 23
                    text: i18n("Change Wallpaper")
                    font.pixelSize: 14
                    color: textcolor
                    font.family: "SourceHanSansSC-Regular"
                }

                Kirigami.Icon {
                    anchors.verticalCenter: appearanceRect.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 498

                    height: 16
                    width: 16
                    source: "go-next-symbolic"
                }

                Rectangle {
                    id: appearanceBackgrund
                    anchors.fill: parent
                    opacity: 0.2
                    z: 1
                    color: "transparent"
                }
                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    visible:true
                    z: 100
                    onEntered: {
                        appearanceBackgrund.color = "gray"
                    }
                    onExited: {
                        appearanceBackgrund.color = "transparent"
                    }
                    onClicked: {
                        appearanceBackgrund.color = "transparent"
                        kcm.openKCM("kcm_wallpaper")
                    }
                }
            }

            Rectangle{
                anchors.top: parent.top
                anchors.topMargin: 47
                width:parent.width
                color:spaceline
                height:1
            }

            // more settings
            Rectangle {
                id: moreSettingsRect
                color: "transparent"
                anchors.top: parent.top
                anchors.topMargin: 48
                anchors.left: parent.left
                width: parent.width
                height: 47

                Kirigami.Icon {
                    id: moreSettings
                    anchors.verticalCenter: moreSettingsRect.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 23

                    height: 16
                    width: 16
                    source: "more-settings"
                }

                Text {
                    anchors.verticalCenter: moreSettingsRect.verticalCenter
                    anchors.left: moreSettings.right
                    anchors.leftMargin: 23
                    text: i18n("More Appearance Settings")
                    font.pixelSize: 14
                    color: textcolor
                    font.family: "SourceHanSansSC-Regular"
                }

                Kirigami.Icon {
                    id: goToLookandfeel
                    anchors.verticalCenter: moreSettingsRect.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 498

                    height: 16
                    width: 16
                    source: "go-next-symbolic"
                }

                Rectangle {
                    id: moreSettingsBackgrund
                    anchors.fill: parent
                    opacity: 0.2
                    z: 1
                    color: "transparent"
                }
                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    z: 100
                    onEntered: {
                        moreSettingsBackgrund.color = "gray"
                    }
                    onExited: {
                        moreSettingsBackgrund.color = "transparent"
                    }
                    onClicked: {
                        moreSettingsBackgrund.color = "transparent"
                        kcm.openKCM("kcm_lookandfeel")
                    }
                }
            }
        }

        // single click
        Rectangle {
            id: singleClick
            anchors.top: appearance.bottom
            anchors.topMargin: 6
            anchors.horizontalCenter:parent.horizontalCenter
            color: backgroundcolor
            height:54
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10

            Text {
                id: singleLabel
                anchors.top: parent.top
                anchors.topMargin: 3
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nc("landingpages single click", "Clicking files or folders")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            Text {
                anchors.top: singleLabel.bottom
                anchors.topMargin: 3
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nc("landingpages single click", "Open by double-clicking instead")
                font.pixelSize: 14
                color: bordercolor
                font.family: "SourceHanSansSC-Regular"
            }

            QQC2.ComboBox {
                id: accelProfile
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.right: parent.right
                anchors.rightMargin: 44
                width: 192
                height:30
                model: [i18nc("landingpages single click", "Selects them"), i18nc("landingpages single click", "Opens them") ]

                currentIndex: kcm.globalsSettings.singleClick ? 1 : 0
                onCurrentIndexChanged: {
                    kcm.globalsSettings.singleClick = currentIndex == 0 ? false : true
                }

                KCM.SettingStateBinding {
                    configObject: kcm.globalsSettings
                    settingName: "singleClick"
                }
            }
        }

        // Most used
        Rectangle {
            id: mostUsed

            anchors.top: singleClick.bottom
            anchors.topMargin: 9
            anchors.horizontalCenter:parent.horizontalCenter
            visible: recentlyUsedRepeater.count > 0

            color: "transparent"
            height:20
            width:558
            Text {
                anchors.left: parent.left
                text: i18n("Most Used Pages")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
        }


        Rectangle {
            id: mostUsedRect
            anchors.top: mostUsed.bottom
            anchors.topMargin: 6
            anchors.horizontalCenter:parent.horizontalCenter
            color: backgroundcolor
            height:101
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10

            clip: true
            layer.enabled: true
            layer.effect: OpacityMask {
                maskSource: Rectangle {
                    width: mostUsedRect.width
                    height: mostUsedRect.height
                    radius: mostUsedRect.radius
                }
            }

            visible: recentlyUsedRepeater.count > 0

            Repeater {
                id: recentlyUsedRepeater

                model: kcm.mostUsedModel
                delegate:
                    Rectangle {
                    id: mostUsedItem

                    color: "transparent"
                    height: 47
                    width: mostUsedRect.width
                    anchors.left: mostUsedRect.left
                    anchors.top: mostUsedRect.top
                    anchors.topMargin: 46 * index

                    Rectangle{
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        width: parent.width
                        color: spaceline
                        height: 1
                        visible: index >= 1
                    }

                    Kirigami.Icon {
                        id: iconItem
                        anchors.verticalCenter: mostUsedItem.verticalCenter
                        anchors.left: parent.left
                        anchors.leftMargin:  23
                        height: 16
                        width: 16
                        source: model.decoration
                    }

                    Text {
                        id: label
                        anchors.verticalCenter: mostUsedItem.verticalCenter
                        anchors.left: iconItem.right
                        anchors.leftMargin:  23
                        text: model.display
                        font.pixelSize: 14
                        color: textcolor
                        font.family: "SourceHanSansSC-Regular"
                    }

                    Kirigami.Icon {
                        anchors.verticalCenter: mostUsedItem.verticalCenter
                        anchors.top: parent.top
                        anchors.left: parent.left
                        anchors.leftMargin: 498

                        height: 16
                        width: 16
                        source: "go-next-symbolic"
                    }

                    Rectangle {
                        id: mosetUsedItemBackgrund
                        anchors.fill: parent
                        opacity: 0.2
                        z: 1
                        color: "transparent"
                    }
                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        visible: mostUsedRect.visible
                        z: 100
                        onEntered: {
                            mosetUsedItemBackgrund.color = "gray"
                        }
                        onExited: {
                            mosetUsedItemBackgrund.color = "transparent"
                        }
                        onClicked: {
                            mosetUsedItemBackgrund.color = "transparent"
                            kcm.openKCM(model.kcmPlugin)
                        }
                    }
                }
                Component.onCompleted: {
                    mostUsedRect.height = recentlyUsedRepeater.count * 46 + 1
                }

                onItemAdded: (index, item) => {
                                 if (index === 0) {
                                     // Set buddy once, for an item in the first row.
                                     // No, it doesn't work as a binding on children list.
                                     mostUsedRect.Kirigami.FormData.buddyFor = item;
                                 }
                                 //mostUsedPagesSizeGroup.items.push(item);

                                 mostUsedRect.height = recentlyUsedRepeater.model.count * 47
                             }
            }
        }
    }
}
